<%@page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<link href="${apliPath}static/css/default.css" rel="stylesheet"
	type="text/css" />
<link rel="stylesheet" type="text/css"
	href="${apliPath}static/js/themes/metro-solidBlue/easyui.css" />
<link rel="stylesheet" type="text/css"
	href="${apliPath}static/js/themes/icon.css" />
<link type="text/css" rel="stylesheet"
	href="${apliPath}static/css/main.css" />
<script type="text/javascript"
	src="${apliPath}static/js/jquery-1.10.2.js"></script>
<script type="text/javascript"
	src="${apliPath}static/js/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="${apliPath}static/js/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
	$(function() {
		$("#manageRole")
				.datagrid(
						{
							title : "角色列表",
							fit : true,
							border : false,
							toolbar : '#toolBar',
							iconCls : 'icon-search',
							url : "${apliPath}role/queryRolesByPage",
							fitColumns : true,
							striped : true,
							singleSelect : true,
							checkOnSelect : true,
							loadMsg : "数据正在加载中，请稍等...",
							rownumbers : true,
							columns : [ [
									{
										field : "roleId",
										checkbox : true,
									},
									{
										title : "名字",
										field : "name",
										width : 50,
									},
									{
										title : "标示",
										field : "code",
										width : 70,
										sortable : true,
									},
									{

										title : "编辑操作",
										field : "opt",
										width : 80,
										formatter : function(value, row, index) {
											return '<a class="delcls" onclick="delRole('
													+ row.id
													+ ')" href="javascript:void(0)"></a><a class="modifyRolecls" onclick="dig_modifyRoleOpen('
													+ index
													+ ')" href="javascript:void(0)"></a>';
										}

									},
									{

										title : "权限操作",
										field : "opt3",
										width : 80,
										formatter : function(value, row, index) {
											return '<a class="updateAuth" onclick="updateAuth('
													+ row.id
													+ ')" href="javascript:void(0)"></a>';
										}

									} ] ],
							onLoadSuccess : function(data) {
								$('#manageRole').datagrid('hideColumn', 'opt');
								<shiro:hasAnyPermission name='fun:role:del,fun:role:upd'>
								$('#manageRole').datagrid('showColumn', 'opt');
								</shiro:hasAnyPermission>
								<shiro:hasPermission name='fun:role:del'>
								$('.delcls').linkbutton({
									text : '删除',
									plain : true,
									iconCls : 'icon-edit'
								});
								</shiro:hasPermission>
								<shiro:hasPermission name='fun:role:upd'>
								$('.modifyRolecls').linkbutton({
									text : '更新',
									plain : true,
									iconCls : 'icon-edit'
								});
								</shiro:hasPermission>
								$('.updateAuth').linkbutton({
									text : '更新权限',
									plain : true,
									iconCls : 'icon-edit'
								});
							},
							pagination : true,
							pageSize : 6,
							pageList : [ 3, 6, 9, 12 ],
							sortName : "code",
							sortOrder : "DESC",
							remoteSort : false,
							toolbar : [ {
								text : '增加角色',
								iconCls : 'icon-edit',
								handler : function() {
									<shiro:hasPermission name='fun:role:add'>
									$('#dig_modifyRole').dialog('setTitle',
											'添加角色');
									$('#dig_modifyRole').dialog('open');
									</shiro:hasPermission>
									<shiro:lacksPermission name='fun:role:add'>
									$.messager.alert('fuckoff',
											'you do not have permission！',
											'info');
									</shiro:lacksPermission>
								}
							} ],
						});

	});
	function delRole(drop) {
		$.messager
				.confirm('确认', '您确认想要删除角色吗？',
						function(r) {
							if (r) {
								var dropID = new Array();
								var operationLOG = '删除管理员{';
								if ((typeof drop) == 'number') {
									dropID.push(parseInt(drop));
									operationLOG = operationLOG
											+ $('#manageRole').datagrid(
													'getRows')[rowIndex].code
											+ ',';
								}
								operationLOG = operationLOG.substring(0,
										operationLOG.length - 1);
								operationLOG = operationLOG + '}';
								var transData = {
									drops : dropID.toString(),
									operationLOG : operationLOG
								};
								$
										.post("${apliPath}role/drop",
												transData, function(data) {
													$.messager.show({
														title : "提示框",
														msg : "删除成功",
													});
													$("#manageRole").datagrid(
															"reload");
												});
							} else {
								alert("幸亏你没有删除");
							}
						});

	}
	function updateAuth(id) {
		$.post("${apliPath}resource/queryMenuResourcesTreeByRole", {
			"id" : id
		}, function(data) {
			$("#menuRights").tree({
				data : data,
				animate : true,
				checkbox : true
			});
		});
		$.post("${apliPath}resource/queryFuncResourcesTreeByRole", {
			"id" : id
		}, function(data) {
			$("#funcRights").tree({
				data : data,
				animate : true,
				checkbox : true
			});
		});
		$("#editRoles").window("open");
	}
	function updateAuthDone() {
		var menuRightsNode = $('#menuRights').tree('getChecked',
				[ 'checked', 'indeterminate' ]);
		var menuRights = new Array();
		$.each(menuRightsNode, function(index, node) {
			menuRights.push(node.id);
		});
		var funcRightsNode = $('#funcRights').tree('getChecked',
				[ 'checked', 'indeterminate' ]);
		var funcRights = new Array();
		$.each(funcRightsNode, function(index, node) {
			funcRights.push(node.id);
		});
		$.post("${apliPath}resource/saveAuthByRole", {
			"menuRights" : menuRights.toString(),
			"funcRights" : funcRights.toString(),
			"roleID" : $('#manageRole').datagrid("getSelected").id
		}, function(data) {
			if (!data) {
				$('#editRoles').window('close');
				$.messager.show({
					timeout : 2200,
					title : "修改权限",
					msg : "已经修改"
				});
			}
		});
	}
	//全选或者反选权限树
	function checkRightsTree(treeID) {
		var roots = $('#' + treeID).tree('getRoots');//返回tree的所有根节点数组  
		if ($('#checkAuthFlag').val() == 0) {
			for (var i = 0; i < roots.length; i++) {
				var node = $('#' + treeID).tree('find', roots[i].id);//查找节点  
				$('#' + treeID).tree('check', node.target);//将得到的节点选中  
			}
			$('#checkAuthFlag').val(1);
		} else {
			for (var i = 0; i < roots.length; i++) {
				var node = $('#' + treeID).tree('find', roots[i].id);
				$('#' + treeID).tree('uncheck', node.target);
			}
			$('#checkAuthFlag').val(0);
		}
	}
	//修改管理员信息改变
	function dig_modifyRoleDone() {
		if (!isRoleCodeAlreadyUsed($('#roleCode').textbox('getValue'))) {
			var id = $('#manageRole').datagrid('getRows')[rowIndex].id;
			var name = $('#roleName').textbox('getValue');
			var code = $('#roleCode').textbox('getValue');
			var transData = new Object();
			var manageRoleUrl = '';
			if ($('#hidden_roleType').val() == 'edit') {
				manageRoleUrl = '${apliPath}role/modifyRole';
				transData.id = id;
				transData.name = name;
				transData.code = code;
				transData.operationLOG = '更新角色[' + code + ']：{' + '名字：' + name
						+ '；标识：' + code + '}';
			} else {
				//如果要是添加的话  必须输入密码
				transData.name = name;
				transData.code = code;
				manageRoleUrl = '${apliPath}role/insert';
				transData.operationLOG = '添加角色[' + code + ']：{' + '名字：' + name
						+ '；标识：' + code + '}';
			}
			$.post(manageRoleUrl, transData, function(data) {
				$('#dig_modifyRole').dialog('close');
				$("#manageRole").datagrid("reload");
				$.messager.show({
					timeout : 2200,
					title : "提示框",
					msg : "操作成功",
				});

			});
		}
	}
	//开启角色窗口编辑对话
	function dig_modifyRoleOpen(index) {
		$('#hidden_roleType').val('edit');
		rowIndex = index;
		$('#roleName').textbox('setValue',
				$('#manageRole').datagrid('getRows')[index].name);
		$('#roleCode').textbox('setValue',
				$('#manageRole').datagrid('getRows')[index].code);
		$('#dig_modifyRole').dialog('setTitle', '修改角色');
		$('#dig_modifyRole').dialog('open');
	}
	//每次点击修改操作的时候赋值
	var rowIndex = 0;
	function isRoleCodeAlreadyUsed(codeValue) {
		if ($('#manageRole').datagrid('getRows')[rowIndex].code != codeValue) {
			var returnFlag = false;
			$
					.ajax({
						type : "post",
						url : "${apliPath}role/isRoleCodeAlreadyUsed",
						async : false,
						data : {
							"code" : codeValue,
							id : $('#manageRole').datagrid('getRows')[rowIndex].id
						},
						success : function(data) {
							var roleCodeAlert = document
									.getElementById("roleCodeAlert");
							if (data == false) {
								roleCodeAlert.innerHTML = "<font color='red'>&nbsp;不可用</font>";
								$('#roleCode').next('span').find('input')
										.focus();
								returnFlag = true;
							} else {
								roleCodeAlert.innerHTML = "<font color='green'>&nbsp;可用</font>";
								returnFlag = false;
							}
						}
					});
			return returnFlag;
		} else {
			return false;
		}
	}
</script>
<input type="hidden" id="hidden_roleType">
<table id="manageRole">
</table>
<div id="toolBar" style="float: right">
	<table>
		<tr>
			<td><a href="javascript:void(0)" class="easyui-linkbutton"
				data-options="iconCls:'icon-cancel',plain:true"> 删除</a></td>
			<td><span style="font-size: 12px">查询管理</span>&nbsp;<input
				type="text" value="想多删？多点几次试试。。" /></td>
		</tr>
	</table>
</div>
<div id="editRoles" style="width: 500; height: 400;top: 120"
	class="easyui-window"
	data-options="closed:true,title:'编辑角色',modal:true">
	<input type="hidden" value="0" id="checkAuthFlag">
	<div class="easyui-layout" data-options="fit:true">
		<div data-options="region:'west',split:true"
			style="width: 50%; padding-left: 10px">
			<div style="float: right; margin: 15">
				<a class="easyui-linkbutton" href="javascript:void(0)"
					onclick="javascript:checkRightsTree('menuRights')"
					style="width: 80px">全选/反选</a>
			</div>
			<h3>菜单权限</h3>
			<ul id="menuRights" class="easyui-tree">
			</ul>
		</div>
		<div data-options="region:'center'" style="padding-left: 10px;">
			<div style="float: right; margin: 15">
				<a class="easyui-linkbutton" href="javascript:void(0)"
					onclick="javascript:checkRightsTree('funcRights')"
					style="width: 80px">全选/反选</a>
			</div>
			<h3>功能权限</h3>
			<ul id="funcRights" class="easyui-tree">
			</ul>
		</div>
		<div data-options="region:'south',border:false"
			style="text-align: right; padding: 10px 20 10;">
			<a class="easyui-linkbutton" data-options="iconCls:'icon-ok'"
				href="javascript:void(0)" onclick="javascript:updateAuthDone()"
				style="width: 80px">提交</a> <a class="easyui-linkbutton"
				data-options="iconCls:'icon-cancel'" href="javascript:void(0)"
				onclick="javascript:$('#editRoles').window('close')"
				style="width: 80px">取消</a>
		</div>
	</div>
</div>
<div id="modifyRoleDIV">
	<div id="dig_modifyRole" class="easyui-dialog" title="角色信息修改"
		style="width: 350px; height: 200px; padding: 10px;top: 150"
		data-options="
		        closed : true,
				toolbar: '#dlg-toolbar',
				buttons: '#dlg-buttons',
				onClose: function(){
				    $('#dig_modifyRole #roleName').textbox('clear');
				    $('#dig_modifyRole #roleCode').textbox('clear');
				    $('#hidden_managerType').val('add');
				}
			">
		<div style="margin: 10 0 10 10">
			角色名称：<input class="easyui-textbox" id="roleName"
				style="width: 65%; height: 30px; padding: 12px"
				data-options="prompt:'roleName',iconCls:'icon-man',iconWidth:38">
		</div>
		<div style="margin: 10 0 10 10">
			角色标识：<input class="easyui-textbox" id="roleCode"
				style="width: 65%; height: 30px; padding: 12px"
				data-options="prompt:'roleCode',iconCls:'icon-man',iconWidth:38,onChange:function(){isRoleCodeAlreadyUsed(this.value)}">&nbsp;<span
				id="roleCodeAlert"></span>
		</div>
	</div>
	<div id="dlg-buttons">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			onclick="javascript:dig_modifyRoleDone()">存起来</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			onclick="javascript:$('#dig_modifyRole').dialog('close')">先取消</a>
	</div>
</div>
